package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.DaoPronostico;
import model.dao.impl.DaoPronosticoImp;

public class ServletSelect extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        try {
            StringBuilder sb = new StringBuilder();
            List<Object[]> list;
            String accion = request.getParameter("action");
                    
            if (accion.equals("cbconfig")) {
                DaoPronostico objcosa = new DaoPronosticoImp();
              //  list = objcosa.config();
               sb= selectC(null, "name=\"config\" id=\"config\" onchange=\"js_itemcolum()\"", response);


            } 
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(sb);
            out.close();
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
            ServletContext context = this.getServletContext();
            RequestDispatcher view = context.getRequestDispatcher("/error.jsp");
            view.forward(request, response);

        }
    }

    private StringBuilder selectC(List<Object[]> list, String atributos, HttpServletResponse response) throws Exception {
        StringBuilder sb = new StringBuilder();
        if (list != null) {
            sb.append("<select style=\"width:222px\" ").append(atributos).append(">");
            for (Object[] fila : list) {
                sb.append("<option  ");
                if (fila[2] != null) {
                    if (fila[2].toString().equals("true")) {
                        sb.append(" selected ");
                    }
                }
                sb.append(" value=\"").append(fila[0]).append("\">");
                sb.append(fila[1]);
                sb.append("</option>");
            }
            sb.append("</select>");
        }
        if (list == null) {
            sb.append("<select style=\"width:222px\" ").append(atributos).append(">");
            sb.append("<option  ");
            sb.append(" value=\"- Sin datos -\">");
            sb.append("--");
            sb.append("</option>");
            sb.append("</select>");
        }


        return  sb;

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
